package filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * Description: 该类是定义 网关的全局过滤器
 *
 * @Autor: Rolin
 * datetime: 04-28-14:59
 */
@Component
public class MyGatewayFilter implements GlobalFilter, Ordered {

    // 自定义全局网关 过滤器；
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String uname = exchange.getRequest().getQueryParams().getFirst("uname");
        if (uname==null){
//            此处表示不放行，拒绝访问；
            System.out.println("非法访问！");
            exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
            return exchange.getResponse().setComplete();
        }
        //将过滤链传递下去；
        return chain.filter(exchange);
    }

   //此处设置过滤链中的单一过滤器的 执行优先级
    @Override
    public int getOrder() {
        return 0;
    }
}
